<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选 -->
      <SubFilter @change="filterChange"></SubFilter>
      <!-- <XtxCheckbox></XtxCheckbox> -->
      <!-- <SubSort></SubSort> -->
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @change="sortChange" />
        <!-- 列表 -->
        <ul>
          <li v-for="i in gooodsList" :key="i.id">
            <GoodsItem :sub="i" />
          </li>
        </ul>
        <!-- 上拉加载 -->
        <!-- 上拉加载组件 -->
        <PullLoad
          :loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="load"
        ></PullLoad>
      </div>
    </div>
  </div>
</template>

<script>
// import { useRoute } from 'vue-router'
import SubBread from './components/sub-bread'
import SubSort from './components/sub-sort'
import GoodsItem from './components/goods-item.vue'
import SubFilter from './components/sub-filter'
import { watch, ref } from 'vue'
import { categoryGoods } from '../../api/category'
import PullLoad from './components/PullLoad.vue'
// import { watch } from 'vue'
export default {
  components: {
    SubBread,
    SubFilter,
    SubSort,
    GoodsItem,
    PullLoad
  },
  props: ['id'],
  setup (props) {
    // const route = useRoute()
    // const id = route.params.id
    let paramsApi = {
      categoryId: props.id,
      page: 1,
      pageSize: 15
    }
    const gooodsList = ref([])
    // 属性筛选
    function filterChange (obj) {
      console.log(obj)
      paramsApi = {
        ...paramsApi,
        ...obj,
        page: 1
      }
      console.log(paramsApi)
      gooodsList.value = []
      load()
    }
    // 排序筛选
    function sortChange (item) {
      console.log(item)
      paramsApi = {
        ...paramsApi,
        ...item,
        sortMethod: item.sortMethod || 'desc'
      }
      gooodsList.value = []
      load()
    }
    // 内容结果列表

    // 上拉加载
    const loading = ref(false)
    const finished = ref(false)

    watch(
      () => props.id,
      () => {
        paramsApi = {
          categoryId: props.id,
          page: 1,
          pageSize: 15
        }
        load()
        // console.log(paramsApi)
      },
      { immediate: true }
    )
    function load () {
      categoryGoods(paramsApi).then(res => {
        //  console.log(res)
        gooodsList.value = res.result.items
      })
    }
    //
    // console.log(paramsApi)
    return { filterChange, gooodsList, sortChange, load, loading, finished }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
